package com.firewolf.mybatis;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MainDriver {

	private SqlSession session;

	@Before
	public void init() throws IOException {
		// 1.创建一个SqlSessionFactory，这里有多种方式
		Reader reader = Resources.getResourceAsReader("conf.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
		// 3.打开一个SqlSession
		session = factory.openSession();

	}

	@After
	public void destory() throws Exception {
		session.close();
	}

	/**
	 * @Description: 在接口中写方法，同时通过注解写语句
	 * @输入和输出示例：
	 * @思路：
	 * @throws IOException
	 */
	@Test
	public void testMap1() throws IOException {
		UserMapper uMapper = session.getMapper(UserMapper.class);
		User u = uMapper.qryUserById(1);
		System.out.println(u);
	}
	
	/**
	 * @Description: 在接口中写方法，xml中写语句
	 * @输入和输出示例：
	 * @思路：
	 */
	@Test
	public void testMap2(){
		UserMapper2 uMapper2 = session.getMapper(UserMapper2.class);
		User u = uMapper2.qryUserById(4);
		System.out.println(u);
	}
	
	/**
	 * @Description: 传递多个参数，在xml中使用下标访问
	 * @输入和输出示例：
	 * @思路：
	 */
	@Test
	public void testPassParam(){
		UserMapper2 uMapper2 = session.getMapper(UserMapper2.class);
		User u = uMapper2.qryUserByIdAndAge(4,32);
		System.out.println(u);
	}
	/**
	 * @Description: 传递多个参数，在xml中使用参数名字访问
	 * @输入和输出示例：
	 * @思路：
	 */
	@Test
	public void testPassParam2(){
		UserMapper2 uMapper2 = session.getMapper(UserMapper2.class);
		User u = uMapper2.qryUserByIdAndAge2(4,32);
		System.out.println(u);
	}
}
